package com.toocms.jinjuhang.ui.advert_list

import android.app.Application
import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.core.graphics.drawable.DrawableCompat
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.VideoData
import com.toocms.jinjuhang.data.center.VideoPointData
import com.toocms.jinjuhang.dialog.hint.HintDialog
import com.toocms.jinjuhang.utils.FragmentUtils
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.event.SingleLiveEvent
import com.toocms.tab.network.ApiTool
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

class AdvertDetailsModel(application: Application, val bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {

    var isGetPoint = false
    var termLong: Long = 0L
    private var logId = ""

    val loadVideoSingleLiveEvent = SingleLiveEvent<VideoData>()

    val isViewEnabledObservableBoolean = ObservableBoolean(true)
    val isCompleteObservableBoolean = ObservableBoolean(false)
    val timeRemainingObservableField = ObservableField<CharSequence>("秒后可以获得奖励")

    val currentTimeObservableField = ObservableField<String>()

    val backObservableField by lazy {
        val content = ResourceUtils.getDrawable(R.mipmap.icon_arrow_left_black).mutate()
        DrawableCompat.setTint(
            content,
            ColorUtils.getColor(com.toocms.tab.R.color.qmui_config_color_white)
        )
        ObservableField<Drawable>(content)
    }

    val backClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (isViewEnabledObservableBoolean.get()) {
            finishFragment()
            return@BindingAction
        }
        HintDialog().apply {
            title = StringUtils.getString(R.string.str_hint)
            content = "现在退出将无法获得积分"
            passive = StringUtils.getString(R.string.str_cancel)
            positive = StringUtils.getString(R.string.str_confirm)
            onPositiveClickListener = {
                finishFragment()
            }
            show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null)
        }
    })

    val viewMoreClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (null != FragmentUtils.findFragment(AdvertListFgt::class.java)) {
            FragmentUtils.popTo(AdvertListFgt::class.java, false)
        } else {
            startFragment(AdvertListFgt::class.java)
        }
    })

    val videoId by lazy {
        bundle?.getString(Constants.BundleKey.KEY_VIDEO_ID) ?: ""
    }


    val data by lazy {
        val result: VideoData? = bundle?.getParcelable(Constants.BundleKey.KEY_VIDEO) ?: null
        return@lazy result
    }

    init {
        data?.apply {
            loadVideoSingleLiveEvent.value = this
            termLong = (term ?: "0").toLong() * 1000L
        }

//        val params = mutableMapOf<String, String?>()
//        params["member_id"] = UserRepository.user.member_id
//        params["video_id"] = videoId
//        requestData(
//            Urls.Center.GET_VIDEO,
//            params,
//            resultClass = VideoData::class.java,
//            isShowLoading = true,
//            result = {
//                loadVideoSingleLiveEvent.value = it
//            })
    }

    override fun onStop() {
        super.onStop()
        setView()
    }


    fun videoPoint() {
        val params = mutableMapOf<String, String?>()
        params["member_id"] = UserRepository.user.member_id
        params["video_id"] = data?.video_id
        params["term"] = data?.term
        requestData(
            Urls.Center.VIDEO_POINT,
            params,
            resultClass = VideoPointData::class.java,
            result = {
                logId = it.log_id
                if (it.points.isNullOrEmpty() || 0f >= (it.points?.toFloat() ?: 0F)) {
                    timeRemainingObservableField.set("无法获得奖励")
                } else {
                    timeRemainingObservableField.set("已获得奖励")
                }
            })
    }


    fun setView() {
        ApiTool.post(Urls.Center.SET_VIEW)
            .add("member_id", UserRepository.user.member_id)
            .add("video_id", videoId)
            .add("log_id", logId)
            .add("term", data?.term)
            .add("complete", if (isCompleteObservableBoolean.get()) "1" else "0")
            .asTooCMSResponse(String::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .request({

            },{

            })
    }

}